//
//  THTabBarController.swift
//  TongHuoDemo
//
//  Created by rimi on 2017/5/2.
//  Copyright © 2017年 rimi. All rights reserved.
//

import UIKit

class THTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 改变按钮字体选中颜色
        tabBar.tintColor = UIColor(displayP3Red: 227/255.0, green: 148/255.0, blue: 170/255.0, alpha: 1.0)
        
        let tabDict = [["vc":"THReadingController","title":"阅读","imageName":"阅读"],
                       ["vc":"THColumnController","title":"专栏","imageName":"专栏"],
                       ["vc":"THTopicController","title":"话题","imageName":"社区"],
                       ["vc":"THActivityController","title":"活动","imageName":"乐活"],
                       ["vc":"THMineController","title":"我的","imageName":"我的"]]
        
        // 添加子控制器
        for dict in tabDict {
            addController(dict)
        }
        
        // 设置标签栏的背景色和透明度
        tabBar.backgroundImage = UIImage()
        tabBar.shadowImage = UIImage()
        tabBar.backgroundColor = UIColor(white: 0.2, alpha: 0.7)
    }
    
    /// 重载添加方法封装
    func addController(_ dict: [String:String]) {
        // 获得系统info.plist
        guard let infoPath = Bundle.main.path(forResource: "Info.plist", ofType: nil),
            let bundleName = NSDictionary(contentsOfFile: infoPath)?["CFBundleName"] else {
                return
        }
        
        let className = "\(bundleName).\(dict["vc"] ?? "")"
        // 使用反射机制将字符串转换为类型
        guard let cls = NSClassFromString(className) as? UIViewController.Type, let imageName = dict["imageName"] else {
            return
        }
        // 获得当前类型
        let childController = cls.init()
        // 标题
        childController.title = dict["title"]
        // nomal图片
        childController.tabBarItem.image = UIImage(named: "\(imageName)未选")
        // 设置字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.boldSystemFont(ofSize: 11)], for: .normal)
        // select图片
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)选中")?.withRenderingMode(.alwaysOriginal)
        // 导航控制器
//        let navVc = UINavigationController(rootViewController: childController)
        
        // 添加
        addChildViewController(childController)
    }


}
